<?php
namespace app\common\traits;
use app\common\facade\UserInfo;
use think\model\concern\SoftDelete;
use think\facade\{
    Config,Request,App,Hook
};
trait Models
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    //定义错误信息
    protected $error;

    /**
     * 用户更新或者添加
     * @param array  $data 数据
     * @param bool   $scene 验证场景
     * @return bool
     */
    public function renew($data=[],$scene='') {
        $data = (is_array($data) && !empty($data)) ? $data : Request::post();
        //验证
        $class_name=get_class();
        $start=strrpos($class_name,'\\')+1;
        if($scene!==false){
            $validate_name=substr($class_name,$start);
            $validate = App::validate($validate_name);
            if($scene!=''){
                $validate->scene($scene);
            }
            if (!$validate->check($data)) {
               $this->error = $validate->getError();
               return false;
            }
        }
        if ((isset($data['id']) && (int)$data['id']>0)){
            $object = $this::update($data);
            $type   =  1;
        }else{
            $object = $this::create($data);
            $type   =  2;
        }
        return $object ? $object : false;
    }
    
    /**
     * 返回模型的错误信息
     * @access public
     * @return string|array
     */
    public function getError()
    {
        return $this->error;
    }

}
